Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor ipsec function #4334

Merged
merged 17 commits into from
Aug 12, 2024
Merged

refactor ipsec function #4334

merged 17 commits into from
Aug 12, 2024

Conversation

changluyi
Copy link
Collaborator

@changluyi changluyi commented Jul 24, 2024

Pull Request

What type of this PR

Examples of user facing changes:

  • Features
    image

  • Bug fixes

  • Docs

  • Tests

Which issue(s) this PR fixes

Fixes #(issue-number)

@zhangzujian
Copy link
Member

zhangzujian commented Jul 24, 2024

Could you please add an e2e test case for this feature? I'm working on security: run as non-root user, which may break this function.

@zhangzujian zhangzujian added the feature New network feature label Jul 24, 2024
@changluyi
Copy link
Collaborator Author

Could you please add an e2e test case for this feature? I'm working on security: run as non-root user, which may break this function.

e2e case will be added

@changluyi changluyi force-pushed the ipsec_refactor2 branch 2 times, most recently from 8834515 to bd106fe Compare July 29, 2024 04:37
Makefile Outdated Show resolved Hide resolved
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
Signed-off-by: clyi <[email protected]>
updateBanpQueue workqueue.RateLimitingInterface
deleteBanpQueue workqueue.RateLimitingInterface
banpKeyMutex keymutex.KeyMutex
csrLister certListerv1.CertificateSigningRequestLister
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need a empty line to separate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1

Signed-off-by: clyi <[email protected]>
@changluyi changluyi merged commit 502a90e into master Aug 12, 2024
62 checks passed
@changluyi changluyi deleted the ipsec_refactor2 branch August 12, 2024 01:38
zcq98 pushed a commit to zcq98/kube-ovn that referenced this pull request Aug 12, 2024
refactor ovn ipsec function

Signed-off-by: clyi <[email protected]>
// From this, point we are dealing with an approved CSR
// Get CA in from ovn-ipsec-ca
caSecret, err := c.config.KubeClient.CoreV1().Secrets("kube-system").Get(context.TODO(), util.DefaultOVNIPSecCA, metav1.GetOptions{})
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err


// Decode the certificate request from PEM format.
certReq, err := decodeCertificateRequest(csr.Spec.Request)
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err


// Decode the CA certificate from PEM format.
caCert, err := decodeCertificate(caSecret.Data["cacert"])
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

}

caKey, err := decodePrivateKey(caSecret.Data["cakey"])
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

// Create a new certificate using the certificate template and certificate.
// We can then sign this using the CA.
signedCert, err := signCSR(newCertificateTemplate(certReq), certReq.PublicKey, caCert, caKey)
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err


// Encode the certificate into PEM format and add to the status of the CSR
csr.Status.Certificate, err = encodeCertificates(signedCert)
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

return nil
}

if err := c.updateCsrStatus(csr); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

Message: message,
})

if err := c.updateCsrStatus(csr); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err


func newCertificateTemplate(certReq *x509.CertificateRequest) *x509.Certificate {
serialNumber, err := rand.Int(rand.Reader, big.NewInt(1<<62))
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err


func signCSR(template *x509.Certificate, requestKey c.PublicKey, issuer *x509.Certificate, issuerKey c.PrivateKey) (*x509.Certificate, error) {
derBytes, err := x509.CreateCertificate(rand.Reader, template, issuer, requestKey, issuerKey)
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

return nil, err
}
certs, err := x509.ParseCertificates(derBytes)
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err


func decodeCertificateRequest(pemBytes []byte) (*x509.CertificateRequest, error) {
block, _ := pem.Decode(pemBytes)
if block == nil || block.Type != "CERTIFICATE REQUEST" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

func decodeCertificate(pemBytes []byte) (*x509.Certificate, error) {
block, _ := pem.Decode(pemBytes)
if block == nil || block.Type != "CERTIFICATE" {
err := errors.New("PEM block type must be CERTIFICATE")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

func decodePrivateKey(pemBytes []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(pemBytes)
if block == nil || block.Type != "PRIVATE KEY" {
fmt.Println(block.Type)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

}

key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

func encodeCertificates(certs ...*x509.Certificate) ([]byte, error) {
b := bytes.Buffer{}
for _, cert := range certs {
if err := pem.Encode(&b, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

func getOVSSystemID() (string, error) {
cmd := exec.Command("ovs-vsctl", "--retry", "-t", "60", "get", "Open_vSwitch", ".", "external-ids:system-id")
output, err := cmd.Output()
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err


func checkCertExpired() (bool, error) {
certBytes, err := os.ReadFile(ipsecCertPath)
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

}

cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err


func generateCSRCode() ([]byte, error) {
cn, err := getOVSSystemID()
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

klog.Infof("ovs system id: %s", cn)
cmd := exec.Command("openssl", "genrsa", "-out", ipsecPrivKeyPath, "2048")
err = cmd.Run()
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err


func linkCACertToIPSecDir() error {
cmd := exec.Command("ln", "-s", ipsecCACertPath, "/etc/ipsec.d/cacerts/")
if err := cmd.Run(); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

func clearCACertToIPSecDir() error {
// clear /etc/openvswitch/keys/ipsec-cacert.pem
cmd := exec.Command("rm", "-f", "/etc/openvswitch/keys/ipsec-cacert.pem")
if err := cmd.Run(); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

}

func initIPSecKeysDir() error {
if err := os.MkdirAll(ipsecKeyDir, 0o755); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

}

func clearIPSecKeysDir() error {
if err := os.Remove(ipsecPrivKeyPath); err != nil && !os.IsNotExist(err) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

}
} else {
checkCertExpired, err := checkCertExpired()
if err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need log err

@bobz965
Copy link
Collaborator

bobz965 commented Aug 12, 2024

@changluyi 大佬,我感觉有些err log 还是就近打印更好些,后面有空麻烦补充下

@changluyi
Copy link
Collaborator Author

好的 我有空加下

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New network feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants